<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller
{
        function __construct()
        {
		parent::__construct();
		$this->load->model('Users');
                $this->load->model('Administration');
		$this->load->model('mi_facebook_model');
	}
        
	function index()
	{
		//$this->load->view('layout/header') ; 
                //$this->load->view('login');
		//$this->load->view('layout/footer');
	}
	
	function profile($id)
        {
                
                //$data['user']= $this->User->get_data($id);
                //$this->load->view('layout/header') ; 
                //$this->load->view('user/profile',$data);
                //$this->load->view('layout/footer');
        }
        
        function main()
        {
            if($this->session->userdata('logged_in'))
                {
                $this->load->view('layout/header') ; 
                $this->load->view('maingame');
                $this->load->view('layout/footer');
                }
                else
                {
                    redirect("/");
                }
        }
        
	private $appId;
	private $secret;
	
        function validate()
        {
            $this->form_validation->set_rules('user', 'Correo electrónico', 'required');
	    $this->form_validation->set_rules('pass', 'Password', 'required');
            
            
            if($this->form_validation->run() == FALSE)
		  {
			 $this->load->view('layout/header') ;
                         $data['fecha'] = $this->Administration->return_date_main();
                         $data['activities'] = $this->Administration->return_activity();
                         $data['positions'] = $this->Administration->return_positions();
				
			#-----------------
					
			$this->appId = '712944462084224';
			$this->secret = '58b2806877539684ebedfbe88cb9d226';
			$fb_config = array(
			    'appId' => $this->appId,
			    'secret' => $this->secret
			);
		 
			$this->load->library('facebook', $fb_config);
		 
			$user_id = $this->facebook->getUser();
			$token = $this->facebook->getAccessToken();
		 
			$data['login_url'] = $this->facebook->getLoginUrl(array(
				'scope' => 'email,user_birthday,publish_stream,offline_access', 'redirect_uri' => 'http://prode.indev.com.ar/login/byFacebook'));
			
			#-----------------
				
                         $this->load->view('login',$data);
			 $this->load->view('layout/footer');
		  }
		  else
		  {
                        $var = $this->Users->ValidateSession($this->input->post());
                            if($var != 0)
				    {
					     $this->session->set_userdata($var);
                                             redirect("login/main"); 
				    }
				    else
				    {
                                        $this->load->view('layout/header') ; 
                                        $this->load->view('layout/header') ;
                                        $data['fecha'] = $this->Administration->return_date_main();
                                        $data['activities'] = $this->Administration->return_activity();
                                        $data['positions'] = $this->Administration->return_positions();
					
					#-----------------
					
					$this->appId = '712944462084224';
					$this->secret = '58b2806877539684ebedfbe88cb9d226';
					$fb_config = array(
					    'appId' => $this->appId,
					    'secret' => $this->secret
					);
				 
					$this->load->library('facebook', $fb_config);
				 
					$user_id = $this->facebook->getUser();
					$token = $this->facebook->getAccessToken();
				 
					$data['login_url'] = $this->facebook->getLoginUrl(array(
						'scope' => 'email,user_birthday,publish_stream,offline_access', 'redirect_uri' => 'http://prode.indev.com.ar/login/byFacebook'));
					
					#-----------------
					
					
                                        $this->load->view('login',$data);
                                        $this->load->view('layout/footer');
				    }  
                  }
            
        }
        
        function LogOut()
        {
            $var = array('user_data' => null,'username' => null,'email' => null, 'logged_in' => false);
            $this->session->set_userdata($var);
            $this->session->unset_userdata(null);
            $this->session->sess_destroy();
            redirect("/");    
        }
	
	function byFacebook()
	{
		$this->appId = '712944462084224';
		$this->secret = '58b2806877539684ebedfbe88cb9d226';
		$fb_config = array(
		    'appId' => $this->appId,
		    'secret' => $this->secret
		);
		$this->load->library('facebook', $fb_config);
 
		$user_id = $this->facebook->getUser();
		$token = $this->facebook->getAccessToken();
 
                $data['user_profile'] = $this->facebook->api('/me');
		$data['usuario'] = $this->mi_facebook_model->datos_usuario();
		
		$var = $this->Users->InsertFromFacebook($data);
		$this->session->set_userdata($var);
		
		$this->load->view('layout/header') ; 
                $this->load->view('maingame');
                $this->load->view('layout/footer');
 
	}
}
